<template>
	<u-image class="uImg" :width="realWidth" :height="realHeight" :src="realSrc" :shape="circle?'circle':'square'"
		:border-radius="radius">
		<image slot="error" src="https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg">
		</image>
	</u-image>
</template>

<script>
	export default {
		name: "q-image",
		props: {
			src: {
				type: String,
				default: "",
			},
			height: {
				type: [Number, String],
				default: "100%",
			},
			width: {
				type: [Number, String],
				default: "100%",
			},
			circle: {
				type: Boolean,
				default: false,
			},
			radius: {
				type: [Number, String],
				default: 0,
			},
		},
		computed: {
			realSrc() {
				if (!this.src) {
					return;
				}
				let real_src = this.src.split(",")[0];
				if (this.isExternal(real_src)) {
					return real_src;
				}
				return this.$img + real_src;
			},
			realWidth() {
				return typeof this.width == "string" ? this.width : `${this.width}px`;
			},
			realHeight() {
				return typeof this.height == "string" ? this.height : `${this.height}px`;
			},
		},
		data() {
			return {

			};
		},
		methods: {
			isExternal(path) {
				return /^(https?:|mailto:|tel:)/.test(path);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.uImg {
		width: 100%;
		height: 100%;
	}

	::v-deep .u-image__error {
		>uni-image {
			width: 100% !important;
			height: 100% !important;
		}
	}
</style>
